var contextPath = document.location.pathname.substr(0, document.location.pathname.substr(1).indexOf("/") + 1);
/**
 * 注册表单验证
 * @param url
 */
function checkInforForm(formId){
	 	$("#"+formId).validate({
	 		onfocusout: function (element, event) {
	 		    if (event.which === 9 && this.elementValue(element) === "") {
	 		        return;
	 		    } else {
	 		        this.element(element);
	 		    }
	 		},
	 		rules: {
 			   cooperName: {
		 		    required: true,
		 		    remote:{       //验证用户名是否存在
		               type:"POST",
		               url:contextPath+"/common/checkCooperName",             
		               data:{cooperName:function(){return $("#cooperName").val();}} 
		              } 
	            },
	 		   province: {
		 		    required: true
	 		   },
	 		   city: {
		 		    required: true
	 		   },
	 		   address: {
		 		    required: true
	 		   },
	 		   phone: {
		 		    required: true,
		 		    isPhone:true
	 		   },
	 		   email: {
		 		    required: true,
		 		    email: true
	 		   },
	 		  postcode: {
		 		    required: true,
		 		    isPostcode:true
	 		   },
	 		  site: {
		 		    required: true,
		 		    isSite:true
	 		   },
	 		  contactPerson: {
		 		    required: true
	 		   },
	 		  contactPhone: {
		 		    required: true,
		 		    isPhone:true
	 		   },
	 		   password: {
		 		    required: true,
		 		    minlength: 5
	 		   },
	 		   confirm_password: {
		 		    required: true,
		 		    minlength: 5,
		 		    equalTo: "#password"
	 		   }
 		    },
	        messages: {
	        	cooperName: {
	        		required: "请输入姓名",
	        		remote: "用户名已存在"
	        	},
	        	province: {
	        		required: "请选择省",
	        	},
	        	city: {
	        		required: "请选择市",
	        	},
	        	address: {
	        		required: "请输入详细地址",
	        	},
	        	phone: {
	        		required: "请输入联系电话",
	        	},
	 		   email: {
		 		    required: "请输入Email地址",
		 		    email: "请输入正确的email地址"
	 		   },
	 		   postcode: {
	        		required: "请输入邮编",
	        	},
	        	site: {
	        		required: "请输入网站",
	        	},
	        	contactPerson: {
	        		required: "请输入紧急联系人姓名",
	        	},
	        	contactPhone: {
	        		required: "请输入紧急联系人电话",
	        	},
	 		   password: {
		 		    required: "请输入密码",
		 		    minlength: "密码不能小于5个字 符"
	 		   },
	 		   confirm_password: {
		 		    required: "请输入确认密码",
		 		    minlength: "确认密码不能小于5个字符",
		 		    equalTo: "两次输入密码不一致"
	 		   },
 		   },
	        submitHandler:function(form){
	            form.submit();
	        }
	    });
	 	$("#reset").click(function() {
            validator.resetForm();
        });
	};
	
	jQuery.validator.addMethod("isPhone", function(value,element) {
 		var mobile = /^1\d{10}$/;
 		var tel = /^d{3,4}-?d{7,9}$/;
 		return this.optional(element) || (tel.test(value) || mobile.test(value));
 		}, "请正确填写您的联系电话");
	
	jQuery.validator.addMethod("isPostcode", function(value,element) {
 		var postcode = /^[0-9]{6}$/;
 		return this.optional(element) || postcode.test(value);
 		}, "请正确填写邮编");
	
	jQuery.validator.addMethod("isSite", function(value,element) {
 		var site = /^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/;
 		return this.optional(element) || site.test(value);
 		}, "请正确填写网站");
	
	
	
function checkPasswordForm(formId){
 	$("#"+formId).validate({
 		onfocusout: function (element, event) {
 		    if (event.which === 9 && this.elementValue(element) === "") {
 		        return;
 		    } else {
 		        this.element(element);
 		    }
 		},
 		rules: {
 		   password: {
	 		    required: true,
	 		    minlength: 5
 		   },
 		   confirm_password: {
	 		    required: true,
	 		    minlength: 5,
	 		    equalTo: "#password"
 		   }
	    },
        messages: {
 		   password: {
	 		    required: "请输入密码",
	 		    minlength: "密码不能小于5个字 符"
 		   },
 		   confirm_password: {
	 		    required: "请输入确认密码",
	 		    minlength: "确认密码不能小于5个字符",
	 		    equalTo: "两次输入密码不一致"
 		   },
	   },
        submitHandler:function(form){
            form.submit();
        }
    });
 	$("#reset").click(function() {
        validator.resetForm();
    });
};


function checkMessForm(formId){
 	return $("#"+formId).validate({
 		onfocusout: function (element, event) {
 		    if (event.which === 9 && this.elementValue(element) === "") {
 		        return;
 		    } else {
 		        this.element(element);
 		    }
 		},
 		rules: {
	 		   phone: {
		 		    required: true,
		 		    isPhone:true
	 		   },
	 		   password: {
		 		    required: true,
		 		    minlength: 5
	 		   }
		    },
        messages: {
	        	phone: {
	        		required: "请输入联系电话",
	        	},
	 		   password: {
		 		    required: "请输入密码",
		 		    minlength: "密码不能小于5个字 符"
	 		   }
		   },
    }).form();
};



/**
 * 发送短信
 */
function messCodeBtn(formId){
   $.ajax({
  	  url : contextPath+"/common/getMessCode",
      type:"post",
      datatype:"json",
      data : {"phone":$("#phone").val()},
      success:function(response){
    	  if(response.status){
    		  phoneCode(60,'messCodeBtn');
        	  $("#messInfo").html("已向您的手机发送验证码，请及时查收。");
        	  $("#codeId").val(response.codeId);
    	  }else{
    		  $("#messInfo").html(response.failMess)
    	  }
    	  
      }
	}); 
};

/**
 * 验证验证码
 */
function messSubmit(){
	$.ajax({
		type:"post",
		url:contextPath+"/common/checkMessCode",
		data:{"code":$("#messCode").val(),
			  "password":$("#password").val(),
			  "phone":$("#phone").val(),
			  "codeId":$("#codeId").val()
			},
		success:function(response){
			if(response.status== "type1"){
				$("#messInfo").html("验证码超时请重新发送！");
			}else if(response.status== "type2"){
				$("#messInfo").html("验证码错误请重新输入！");
			}else{
				setTimeout(window.location.href = contextPath+"/common/loginPortal",5);
			}
		}
	});
};


//验证码定时器调用
function phoneCode(time,btn){
	if(time>0){
		$("#"+btn).val(time+"秒后重新获取");
		setTimeout(function(){
			phoneCode(time-1,btn);
		},1000);
		$("#"+btn).attr("disabled",true);
	}else{
		$("#"+btn).val("获取验证码");
		$("#"+btn).attr("disabled",false);
	}
}

